import {
	checkOssClient,
	checkOssVideoClient,
	initOssClient,
	initOssVideoClient
} from '../utils/upload';


const showfileMixin = {
	methods: {
		async getSignatureUrl(path, bucket = "newxuejingres") {
			// 检查是否已有 Oss Client
			// 检查是否已有 Oss Client
			if (bucket == "newxuejingimage") {
				var client = checkOssClient();
				if (client === null) {
					try {
						var res = await this.$frontaxios.get(this.$front.getStsToken);
						if (res.code == 1) {
							let oss = res.data;
							if (oss.statusCode == 200) {
								client = initOssClient(
									oss['accessKeyId'],
									oss['accessKeySecret'],
									oss['securityToken'],
									oss['expiration'],
									bucket
								);

							}
						} else {
							this.$message.error('请重新登录');
							this.$router.push('/login');
						}
					} catch (error) {
						this.$message.error(`${error}`);
						return;
					}
				}
				try {
					const res1 = await client.signatureUrl(path); //client.getObejectUrl不加密获取
					return res1;
				} catch (error) {
					this.$message.error(`${error}`);
				}

			} else if (bucket == "newxuejingres") {
				var videoclient = checkOssVideoClient();
				if (videoclient === null) {
					try {
						var res = await this.$frontaxios.get(this.$front.getStsVideoToken);
						//console.log(res);
						if (res.code == 1) {
							let oss = res.data;
							if (oss.statusCode == 200) {
								videoclient = initOssVideoClient(
									oss['accessKeyId'],
									oss['accessKeySecret'],
									oss['securityToken'],
									oss['expiration'],
									bucket
								);
							}
						} else {
							this.$message.error('请重新登录');
							this.$router.push({name:'my',query:{islogin:1}});
						}
					} catch (error) {
						this.$message.error(`${error}`);
						return;
					}
				}
				//console.log(videoclient);return;
				try {
					const res1 = await videoclient.signatureUrl(path); //client.getObejectUrl不加密获取
					return res1;
				} catch (error) {
					this.$message.error(`${error}`);
				}

			}
		}
	}
}

export default showfileMixin;
